<template>
  <div class="dropdown" ref="dropdownRef">
    <a
      href
      class="dropdown-toggle btn btn-outline-light"
      @click.prevent="toggleOpen"
      >{{ title }}</a
    >
    <ul v-show="isOpen" class="dropdown-menu">
      <li
        v-for="(item, index) in pathList"
        :key="index"
        :class="['dropdown-item', { 'is-disabled': disabled }]"
      >
        <router-link class="dropdown-link" :to="item.path">{{
          item.name
        }}</router-link>
      </li>
    </ul>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, watch } from "vue";
import useclickoutside from "@/hooks/useClickoutside";
export default defineComponent({
  name: "Dropdown",
  props: {
    title: {
      type: String,
      required: true,
    },
    pathList: {
      type: Array,
      required: true,
    },
  },
  setup() {
    const isOpen = ref(false);
    const toggleOpen = () => {
      isOpen.value = !isOpen.value;
    };
    const dropdownRef = ref<null | HTMLElement>(null);
    const isclickoutside = useclickoutside(dropdownRef);
    // isclickoutside 在下拉框展开的情况下，点击外面区域，下拉框收起
    watch(isclickoutside, () => {
      if (isclickoutside.value && isOpen.value) {
        isOpen.value = false;
      }
    });
    return {
      isOpen,
      toggleOpen,
      dropdownRef,
    };
  },
});
</script>
<style lang="scss" scoped>
.dropdown {
  position: relative;
}
.dropdown-toggle {
  white-space: nowrap;
  &::after {
    display: inline-block;
    margin-left: 0.255em;
    vertical-align: 0.255em;
    content: "";
    border-top: 0.3em solid;
    border-right: 0.3em solid transparent;
    border-bottom: 0;
    border-left: 0.3em solid transparent;
  }
}
.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 99;
  display: block;
  min-width: 10rem;
  padding: 6px 0;
  margin: 0.25rem 0 0;
  font-size: 1rem;
  color: #212529;
  text-align: left;
  list-style: none;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 0.25rem;
}
// 项
.dropdown-item.is-disabled a {
  color: #6c757d;
  cursor: not-allowed;
  background-color: transparent;
}
// a标签router-link
.dropdown-link {
  display: block;
  padding: 6px 0;
  font-weight: 400;
  border-bottom: rgba(0, 0, 0, 0.3) solid 1px;
  color: #212529;
  text-align: center;
  text-decoration: none;
  white-space: nowrap;
  background-color: transparent;
}
.dropdown-link:last-child {
  border-bottom: none;
}
</style>
